package com.hht.toolanduilibs

import android.app.Activity
import android.app.Service
import android.content.ActivityNotFoundException
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import android.widget.Toast
import androidx.annotation.RequiresApi
import java.text.SimpleDateFormat
import java.util.*

/**
 * 所有扩展方法
 * */

fun String.showLog(lv: Int = 4, tag: String = "Hogan") {
    when (lv) {
        1 -> Log.v(tag, "${getTag()}  $this")
        2 -> Log.d(tag, "${getTag()}  $this")
        3 -> Log.w(tag, "${getTag()}  $this")
        4 -> Log.e(tag, "${getTag()}  $this")
        else -> Log.i(tag, "${getTag()}  $this")
    }
}

//获得当前类名，方法名，行名
private fun getTag(): String {
    val caller = Throwable().stackTrace[3] //可变，参数不同，这个数值不一样
    var tag = "%s.%s(L:%d)"
    var callerClazzName = caller.className
    callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1)
    tag = String.format(tag, callerClazzName, caller.methodName, caller.lineNumber)
    return tag
}

fun Long.dataFormat(): String {
    val sdf = SimpleDateFormat("yyyyMMddHHmmss")
    return sdf.format(Date(this))
}

fun String.showToast(context: Context, duration: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(context, this, duration).show()
}

fun Context.startToast(message: String, duration: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(this, message, duration).show()
}

fun Context.sendBroadcast(action: String) {
    val intent = Intent()
    intent.action = action
    sendBroadcast(intent)
}

fun Context.sendBroadcastExt(block: Intent.() -> Unit) {
    val intent = Intent()
    intent.block()
    sendBroadcast(intent)
}

fun Context.startService(pkg: String, className: String) {
    val intent = Intent()
    val componentName = ComponentName(pkg, className)
    intent.component = componentName
    startService(intent)
}

fun Context.stopService(pkg: String, className: String) {
    val intent = Intent()
    val componentName = ComponentName(pkg, className)
    intent.component = componentName
    stopService(intent)
}

fun Context.startService(pkg: String, className: String, block: Intent.() -> Unit) {
    val intent = Intent()
    val componentName = ComponentName(pkg, className)
    intent.component = componentName
    intent.block()
    startService(intent)
}

fun Context.stopService(pkg: String, className: String, block: Intent.() -> Unit) {
    val intent = Intent()
    val componentName = ComponentName(pkg, className)
    intent.component = componentName
    intent.block()
    stopService(intent)
}


fun Context.stopService(pkg: String, className: String, vararg extras: Pair<String, Any>) {
    val intent = Intent()
    val componentName = ComponentName(pkg, className)
    intent.component = componentName
    extras.forEach { (key, value) ->
        when (value) {
            is String -> intent.putExtra(key, value)
            is Int -> intent.putExtra(key, value)
            is Boolean -> intent.putExtra(key, value)
            // 添加其他数据类型的处理逻辑
        }
    }
    stopService(intent)
}

@RequiresApi(Build.VERSION_CODES.CUPCAKE)
fun Context.startActivity(pkg: String, block: Intent.() -> Unit) {
    val intent = packageManager.getLaunchIntentForPackage(pkg)
    intent?.block()
    try {
        startActivity(intent)
    } catch (e: ActivityNotFoundException) {
        e.printStackTrace()
    }
}


fun Context.startActivity(action: String) {
    val intent = Intent()
    intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    intent.action = action
    try {
        startActivity(intent)
    } catch (e: ActivityNotFoundException) {
        e.printStackTrace()
    }
}

fun Context.startActivity(pkg: String, className: String) {
    val intent = Intent()
    val componentName = ComponentName(pkg, className)
    intent.component = componentName
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    try {
        startActivity(intent)
    } catch (e: ActivityNotFoundException) {
        e.printStackTrace()
    }
}


fun Context.startActivity(pkg: String, className: String, block: Intent.() -> Unit) {
    val intent = Intent()
    val componentName = ComponentName(pkg, className)
    intent.component = componentName
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    intent.block()
    try {
        startActivity(intent)
    } catch (e: ActivityNotFoundException) {
        e.printStackTrace()
    }
}

inline fun <reified T : Activity> Context.startActivity(block: Intent.() -> Unit) {
    val intent = Intent(this, T::class.java)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    intent.block()
    try {
        startActivity(intent)
    } catch (e: ActivityNotFoundException) {
        e.printStackTrace()
    }
}

inline fun <reified T : Activity> Context.startActivity() {
    val intent = Intent(this, T::class.java)
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    try {
        startActivity(intent)
    } catch (e: ActivityNotFoundException) {
        e.printStackTrace()
    }
}

inline fun <reified T : Service> Context.startService() {
    val intent = Intent(this, T::class.java)
    try {
        startService(intent)
    } catch (e: ActivityNotFoundException) {
        e.printStackTrace()
    }
}

fun Context.sendBroadcastExt(action: String) {
    val intent = Intent()
    intent.action = action
    sendBroadcast(intent)
}

fun Context.sendBroadcastAsUserExt(action: String) {
    val intent = Intent()
    intent.action = action
    sendBroadcast(intent)
}

fun Context.startActivityByPkg(pkg:String){
    try {
        val intent= packageManager.getLaunchIntentForPackage(pkg)
        intent?.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        startActivity(intent)
    }catch (e:Exception){
        e.printStackTrace()
    }
}



